<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;

class Chat extends Controller
{
    public function kefu($toid){
        $admin=session('admin');
        if(empty($admin)){
            $this->error('您未登录，不能进去后台管理','/admin/index/login');
        }
        $user=\db('user')->field('name')->where('id',$toid)->find();
        $this->assign([
            'formid'=>8,
            'toid'=>$toid,
            'name'=>$user['name']
        ]);
        $this->assign('numleft',5);
        return view();
    }


    //把数据保存到数据库
    public function savechat(){
        
        if (request()->isAjax()){
            $data=input('post.');
            $data['is_read']=0;
            db('chat')->insert(['formid'=>$data['formid'],'toid'=>$data['toid'],'content'=>$data['content'],'is_read'=>$data['is_read'],'date'=>$data['date']]);
        }
    }

    //把内容变为已读
    public function updateisread(){

        if (request()->isAjax()){
            $data=input('post.');
            db('chat')->where(['formid'=>$data['toid'],'toid'=>$data['formid']])->update(['is_read'=>1]);
        }
    }

    public function lst(){
        $admin=session('admin');
        if(empty($admin)){
            $this->error('您未登录，不能进去后台管理','/admin/index/login');
        }
        $this->assign([
            'formid'=>8
        ]);
        $this->assign('numleft',5);
        return view('list');
    }


    public  function get_list(){
        
        $formId=input('formid');
        $info=db('chat')->field('formid,toid')->where('toid',$formId)->group('formid')->select();
        $row=array_map(function ($res){
            // return $res;
            return [
                'head_url'=>$this->get_Url($res['formid']),
                'username'=>$this->get_Name($res['formid']),
                'lastmessage'=>$this->getlastMessage($res['formid'],$res['toid']),
                'countNoread'=>$this->getCountNoread($res['formid'],$res['toid']),
                'date'=>$this->getdate($res['formid'],$res['toid']),
                'head'=>$this->get_Head($res['formid']),
                'chat_page'=>"/admin/chat/kefu?toid={$res['formid']}"
            ];
        },$info);
        return $row;
    }
    private function get_Url($formid){
        $user=db('user')->field('picture')->where('id',$formid)->find();
        return $user['picture'];
    }
    private function get_Name($formid){
        $user=db('user')->field('name')->where('id',$formid)->find();
        return $user['name'];
    }
    //获取头像
    private function get_Head($formid){
        $user=db('user')->field('picture')->where('id',$formid)->find();
        return $user['picture'];
    }
    private function getlastMessage($formid,$toid){
        $one=[
            ['formid','=',$formid],
            ['toid','=',$toid],
        ];
        $two=[
            ['toid','=',$formid],
            ['formid','=',$toid],
        ];
        $mess=db('chat')->whereor([$one,$two])->order('id DESC')->limit(1)->find();
        return $mess['content'];
    }


    private function getCountNoread($formid,$toid){
        return db('chat')->where(['formid'=>$formid,'toid'=>$toid,'is_read'=>0])->count();
    }

    //获取时间
    private function getdate($formid,$toid){
        $one=[
            ['formid','=',$formid],
            ['toid','=',$toid],
        ];
        $two=[
            ['toid','=',$formid],
            ['formid','=',$toid],
        ];
        $date=db('chat')->whereor([$one,$two])->order('id DESC')->limit(1)->find();
        return $date['date'];
    }


    //获取历史数据
    public function gethistorymess(){
        $data=input('post.');

        $result = Db::query("select * from shop_chat where (formid=".$data['formid']." and toid=".$data['toid'].") or (formid=".$data['toid']." and toid=".$data['formid'].")   order by id");
        return $result;

    }

    public function changeNorRead(){
        $formid=input('toid');
        $toid=input('formid');
        db('chat')->where(['formid'=>$formid,'toid'=>$toid])->update(["is_read"=>1]);
    }
}